Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adiciona possibilidade de uso com MongoDB #278

Open
wants to merge 107 commits into
base: main
Choose a base branch
from

Conversation

mbnunes
Copy link

@mbnunes mbnunes commented Jan 29, 2025

Adicionado o uso do MongoDB no projeto. Ainda falta terminar as documentações

See #72

@cuducos cuducos changed the title Mongodb Added Adiciona possibilidade de uso com MongoDB Jan 29, 2025
Copy link
Owner

@cuducos cuducos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revisei apenas a parte de integração (ou seja, ainda não olhei para a implementação do MongoDB em si, db/mongo.go e seus testes), mas muito bom! Feliz demais com essa colaboração!

Deixei alguns comentários, tem coisas que ainda não precisamos mudar antes de dar merge, mas tá muito bom!

.env.sample Outdated Show resolved Hide resolved
cmd/api.go Outdated Show resolved Hide resolved
cmd/cmd.go Outdated Show resolved Hide resolved
cmd/cmd.go Outdated Show resolved Hide resolved
cmd/cmd.go Outdated Show resolved Hide resolved
cmd/transform.go Outdated Show resolved Hide resolved
docker-compose.yml Show resolved Hide resolved
transform/venues.go Outdated Show resolved Hide resolved
transform/venues.go Outdated Show resolved Hide resolved
transform/transform.go Outdated Show resolved Hide resolved
Copy link
Owner

@cuducos cuducos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mais comentários, ainda sem chegar no módulo do MongoDB, ainda focando na integração : )

.env.sample Outdated Show resolved Hide resolved
cmd/api.go Outdated
Comment on lines 65 to 85
uri := os.Getenv("DATABASE_URL")
if strings.HasPrefix(uri, "mongodb://") {
mdb, _ := db.NewMongoDB(mongoDatabase)
if err != nil {
return err
}

defer mdb.Close()
api.Serve(&mdb, port, nr)

} else if strings.HasPrefix(uri, "postgres://") || strings.HasPrefix(uri, "postgresql://") {
pg, err := db.NewPostgreSQL(u, postgresSchema, nr)
if err != nil {
return err
}
defer pg.Close()

api.Serve(&pg, port, nr)
} else {
return fmt.Errorf("unsupported database URI, must start with postgres:// or mongodb://")
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bem melhor : ) Mas ainda dá para evitar repetição usando a interface, algo como:

	var db database
	} else if strings.HasPrefix(uri, "postgres://") || strings.HasPrefix(uri, "postgresql://") {
		db = postgres{DATABASE_URL}
	} else if strings.HasPrefix(uri, "mongodb://") {
		db = mongoDb{DATABASE_URL}
	} else {
	    // ...
        }
	err := api.Server(db)
	if err != nil {
		fmt.Println("Error:", err)
	}

Fiz um exemplo executável se ajudar: https://go.dev/play/p/RAYi89lY1oi

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cuducos eu ainda fico meio confuso com a integraçaão do golang com interfaces, entao eu por enquanto estou estudando mais essa minha deficiencia para retormar. enquanto isso tirei as linhas vazias.

vou retomar essa estrutura mais enxuta depois dos estudos.

cmd/cmd.go Outdated Show resolved Hide resolved
cmd/cmd.go Outdated Show resolved Hide resolved
cmd/transform.go Outdated Show resolved Hide resolved
cmd/cmd.go Outdated Show resolved Hide resolved
Copy link
Owner

@cuducos cuducos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

E, finalmente tive tempo de revisar o mongodb.go — deixei alguns comentários, nada complicado, está bem bom!

Caso inglês seja uma barreira, deixe uns // TODO: translate q eu dou um tapa depois!

db/mongodb.go Outdated Show resolved Hide resolved
db/mongodb.go Outdated Show resolved Hide resolved
db/mongodb.go Outdated Show resolved Hide resolved
db/mongodb.go Outdated Show resolved Hide resolved
db/mongodb.go Outdated Show resolved Hide resolved
db/mongodb.go Outdated Show resolved Hide resolved
db/mongodb.go Outdated Show resolved Hide resolved
db/mongo_test.go Outdated Show resolved Hide resolved
db/mongo_test.go Outdated Show resolved Hide resolved
db/mongo_test.go Outdated Show resolved Hide resolved
.env.sample Outdated Show resolved Hide resolved
cmd/api.go Outdated
Comment on lines 64 to 86

uri := os.Getenv("DATABASE_URL")
if strings.HasPrefix(uri, "mongodb://") {
mdb, _ := db.NewMongoDB(uri)
if err != nil {
return err
}

defer mdb.Close()
api.Serve(&mdb, port, nr)

} else if strings.HasPrefix(uri, "postgres://") || strings.HasPrefix(uri, "postgresql://") {
pg, err := db.NewPostgreSQL(u, postgresSchema, nr)
if err != nil {
return err
}
defer pg.Close()

api.Serve(&pg, port, nr)
} else {
return fmt.Errorf("unsupported database URI, must start with postgres:// or mongodb://")
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Essa implementação precisa mudar usando a interface, para evitar repetição.

cmd/cmd.go Outdated
Comment on lines 70 to 93
if strings.HasPrefix(uri, "mongodb://") {
mdb, err := db.NewMongoDB(uri)
if err != nil {
return err
}
err = mdb.CreateCollection()
if err != nil {
return err
}
err = mdb.CreateIndexes()
if err != nil {
return err
}
defer mdb.Close()
return err
} else if strings.HasPrefix(uri, "postgres://") || strings.HasPrefix(uri, "postgresql://") {
pg, err := db.NewPostgreSQL(u, postgresSchema, nil)
if err != nil {
return err
}
defer pg.Close()
return pg.CreateTable()
} else {
return fmt.Errorf("A URL não contém 'mongodb' nem 'postgres'")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Essa implementação precisa mudar usando a interface, para evitar repetição.

cmd/cmd.go Outdated
Comment on lines 106 to 125
uri := os.Getenv("DATABASE_URL")
if strings.HasPrefix(uri, "mongodb://") {
mdb, err := db.NewMongoDB(uri)
if err != nil {
return err
}
err = mdb.DropCollection()
if err != nil {
return err
}
return err
} else if strings.HasPrefix(uri, "postgres://") || strings.HasPrefix(uri, "postgresql://") {
pg, err := db.NewPostgreSQL(u, postgresSchema, nil)
if err != nil {
return err
}
defer pg.Close()
return pg.DropTable()
} else {
return fmt.Errorf("A URL não contém 'mongodb' nem 'postgres'")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Essa implementação precisa mudar usando a interface, para evitar repetição.

cmd/transform.go Outdated
if cleanUp {
if err := pg.DropTable(); err != nil {
uri := os.Getenv("DATABASE_URL")
if strings.HasPrefix(uri, "mongodb://") {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Essa implementação precisa mudar usando a interface, para evitar repetição.

db/mongodb.go Outdated Show resolved Hide resolved
db/mongodb.go Outdated Show resolved Hide resolved
db/mongodb.go Show resolved Hide resolved
db/mongodb.go Outdated Show resolved Hide resolved
db/mongodb.go Outdated Show resolved Hide resolved
Comment on lines 21 to 51
- name: Iniciar MongoDB no Linux
if: runner.os == 'Linux'
run: |
docker run -d --name mongodb -p 27017:27017 \
-e MONGO_INITDB_ROOT_USERNAME=minhareceita \
-e MONGO_INITDB_ROOT_PASSWORD=minhareceita \
-e MONGO_INITDB_DATABASE=minhareceita \
--health-cmd "mongosh -u minhareceita -p minhareceita --authenticationDatabase admin --eval 'db.runCommand(\"ping\")'" \
--health-interval 10s --health-timeout 5s --health-retries 5 \
mongo:8


- name: Iniciar MongoDB no Windows
if: runner.os == 'Windows'
run: |
docker run -d --name mongodb -p 27017:27017 -e MONGO_INITDB_ROOT_USERNAME=minhareceita -e MONGO_INITDB_ROOT_PASSWORD=minhareceita -e MONGO_INITDB_DATABASE=minhareceita mongo:8

- name: Instalar MongoDB Shell no Windows
if: runner.os == 'Windows'
run: |
Invoke-WebRequest -Uri "https://downloads.mongodb.com/compass/mongosh-2.3.9-win32-x64.zip" -OutFile "mongosh.zip"
Expand-Archive -Path mongosh.zip -DestinationPath "C:\mongosh"
echo "C:\mongosh" | Out-File -Append -Encoding ASCII $env:GITHUB_PATH

- name: Esperar MongoDB estar pronto no Windows
if: runner.os == 'Windows'
run: |
docker ps
exit 1
shell: pwsh

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Essa lógica está desnecessariamente complexa. Não precisamos rodar o MondoDB dentro da máquina em que rodam os testes.

Essa é a arquitetura atual:

architecture-beta
    group c[Test container]
    
    service mr(server)[Minha Receita] in c
    service db(database)[Postgres]

    mr:R -- L:db
Loading

O desejável é ter algo como:

architecture-beta
    group c[Test container]
    
    service mr(server)[Minha Receita] in c
    service db(database)[Postgres]
    service m(database)[MongoDB]

    mr:R -- L:db
    mr:L -- R:m
Loading

O que você está fazendo é:

architecture-beta
    group c[Test container]
    
    service mr(server)[Minha Receita] in c
    service db(database)[Postgres]
    service m(database)[MongoDB] in c

    mr:R -- L:db
    mr:L -- R:m
Loading

Isso traz toda a complexidade de saber como rodar o MongoDB dependendo do sistema operacional em que os testes estão rodando. Isso não faz sentido. O MongoDB é um serviço independente, e deve ser rodado em um container independente, como um serviço. E aí a gente não se preocupa em como rodar o MongoDb.

Existem duas maneiras de fazez isso.

  1. Como você mesmo sugeriu, usando uma Action pronta (vc suegriu essa, mas talvez seja melhor uma mais popular como essa)
  2. Como eu sugeri, usando containers de serviço do GitHub Actions

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

essa popular n roda no windows, eu testei ela. vou de container

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants